package com.smartguard.service;

import com.smartguard.bean.UserInfo;

public class UserInfoService extends BaseService {
	/**
	 * 返回成功与否
	 * @param userInfo
	 * @return
	 */
	public boolean insertUserInfo(UserInfo userInfo){
		if(userInfo != null) 
			return this.getBaseDAO().insert(userInfo);
		else return false;
	}
	
	/**
	 * 返回成功与否
	 * @param username
	 * @param newUserInfo
	 * @return
	 */
	public boolean updateUserInfo(String username, UserInfo newUserInfo){
		if(username == null || "".equals(username)
				|| newUserInfo == null ) 
			return false;
		else{
			UserInfo ref = new UserInfo();
			ref.setUsername(username);
			return this.getBaseDAO().updateOne(ref, newUserInfo);
		}
	}
	
	/**
	 * 返回一个找到的对象。找不到则返回空
	 * @param username
	 * @return
	 */
	public UserInfo findUserInfo(String username){
		if(username == null || "".equals(username)) return null;
		else {
			UserInfo ref = new UserInfo();
			ref.setUsername(username);
			return this.getBaseDAO().findOne(ref);
		}
	}
	
	// for test
	public static void main(String[] args) {
		String userName = "yonghua";
		String contactEmail = "935805490@qq.com";
		UserInfoService infoService = new UserInfoService();
		UserInfo userInfo = infoService.findUserInfo(userName);
		if(userInfo == null) {
			userInfo = new UserInfo();
			userInfo.setUsername(userName);
			infoService.insertUserInfo(userInfo);
		}
		userInfo.setContactEmail(contactEmail);
		userInfo.setMonitor(true);
		infoService.updateUserInfo(userName, userInfo);
		
		System.out.println("insert email success");
		userInfo = infoService.findUserInfo(userName);
		if (userInfo == null) System.out.println("userInfo is null");
		System.out.println(userInfo.getContactEmail());
	}
}
